﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace Server
{
    /// <summary>
    /// Класс для работы с xml
    /// </summary>
    class ParserXML
    {
        /// <summary>
        /// Порт
        /// </summary>
        public string Port { get; set; }

        /// <summary>
        /// IP
        /// </summary>
        public string Ip { set; get; }
        
        /// <summary>
        /// Максимальное количество очереди соединений
        /// </summary>
        public string CountConnects { get; set; }

        /// <summary>
        /// Задержка таймера в милисекундах
        /// </summary>
        public string TimerInterval { get; set; }

        /// <summary>
        /// Тип соедирнения
        /// </summary>
        public string TypeConnection { get; set; }

        public ParserXML()
        {

        }

        /// <summary>
        /// Загрузка и парсинг XML.
        /// </summary>
        /// <returns>Список данных</returns>
        public void LoadXML()
        {
            ServerSettings settings = new ServerSettings();
            //загрузка xml
            XDocument pxml = XDocument.Load(settings.DefaultFileName);

            //парсинг xml с помощью linq
            var serverSetting = (from data in pxml.Descendants(settings.MainElement)
                                 select new ParserXML()
                                 {
                                     Port = data.Element(settings.PottElement).Value,
                                     Ip = data.Element("Ip").Value,
                                     CountConnects = data.Element(settings.CountConnectsElement).Value,
                                     TimerInterval = data.Element(settings.TimerIntervalElement).Value,
                                     TypeConnection = data.Element(settings.TypeConnectionElement).Value
                                 }).First();
            this.Port = serverSetting.Port;
            this.Ip = serverSetting.Ip;
            this.CountConnects = serverSetting.CountConnects;
            this.TimerInterval = serverSetting.TimerInterval;
            this.TypeConnection = serverSetting.TypeConnection;
        }

        /// <summary>
        /// Создание файла настроек.
        /// </summary>
        public void CreateXMLSettings()
        {
            ServerSettings settings = new ServerSettings();
            XElement xml = new XElement(settings.MainElement, new XElement(settings.PottElement, settings.DefaultPort),
                                                          new XElement(settings.CountConnectsElement, settings.DefaultCountConnects),
                                                          new XElement(settings.TimerIntervalElement, settings.DefaultTimerInterval));
            xml.Save(settings.DefaultFileName);
        }
    }
}
